// components/comCard/comCard.js
const tool = require("../../utils/tool")
const config = require("../../config")

Component({

  /**
   * 组件的属性列表
   */
  properties: {
    item: {
      type: Object,
      value: undefined
    },
    type: {
      type: String,
      value: 'normal'
    },
    statusBarHeight: {
      type: String,
      value: '0'
    },
    contentHeight: {
      type: String,
      value: '0'
    },
    loginedUserInfo: {
      type: Object,
      value: '0'
    },
    showCL: {
      type: Boolean,
      value: false
    },
    showPJ: {
      type: Boolean,
      value: false
    },
    showSH: {
      type: Boolean,
      value: false
    },
    showCH: {
      type: Boolean,
      value: false
    },
    typeSimpleShowBorder:{
      type: Boolean,
      value: true
    },
    showMC:{
      type: Boolean,
      value: false
    },
  },

  attached(){
    tool.setThemeColor(this)
    this.setData({
      sysImagURLPrefix:wx.getStorageSync('sysImagURLPrefix'),
      typeSimpleShowBorder:this.properties.typeSimpleShowBorder,
      showMC:this.properties.showMC
    })
  },

  /**
   * 组件的初始数据
   */
  data: {
    showOPForm:false,
    OPFormType:1,
    sat:0,
    input:'',
    img:[],
    mcSelected:false,
    cyy:['已处理','已清理','已修复','已通知查看'],
    cyybh:['重新处理'],
  },

  observers:{
    'showMC'(e){
      if(e==false){
        this.setData({
          mcSelected:false
        })
      }
    }
  },

  /**
   * 组件的方法列表
   */
  methods: {

    setcyy(e){
      this.setData({
        input:this.data.input + e.currentTarget.dataset.item
      })
    },

    enableMC(){
      if(this.properties.loginedUserInfo.type!='admin')return
      this.triggerEvent('enableMC',undefined)
      this.setMCSelected({
        currentTarget:{
          dataset:{
            value:true
          }
        }
      })
    },

    setMCSelected(e){
      this.setData({
        mcSelected:e.currentTarget.dataset.value
      })
      if(e.currentTarget.dataset.value){
        this.triggerEvent('mcAddItem',this.properties.item.id)
        
      }else{
        this.triggerEvent('mcDeleteItem',this.properties.item.id)
      }
    },

    imgClick(e){
      wx.showActionSheet({
        itemList: ['预览','删除'],
        success:res=>{
          switch (res.tapIndex) {
            case 0:
              wx.previewImage({
                urls: this.data.img,
                current: e.currentTarget.dataset.url
              })
              break;
            case 1:
              let re = this.data.img
              let ttt = []
              for(let i=0;i<re.length;i++){
                if(re[i]==e.currentTarget.dataset.url)continue
                ttt.push(re[i])
              }
              this.setData({
                img:ttt
              })
              break;
          }
        }
      })
    },

    async selectFileAndCmps(){
      let re = await tool.selectFile(9 - this.data.img.length)
      wx.showLoading({ mask:true,
        title: '正在压缩',
      })
      for(let i=0;i<re.length;i++){
        re[i] = await tool.comImg(re[i])
      }
      let t = this.data.img
      for(let i=0;i<re.length;i++){
        t.push(re[i])
      }
      this.setData({
        img:t
      })
      wx.hideLoading()
    },

    mpk(){
      tool.showComPhoneNumberList(this.data.statusBarHeight,this.data.contentHeight,this.data.loginedUserInfo,this.data.item.id)
    },

    async SHSubmit(){
      if(this.data.input==''){
        tool.showToast('请输入驳回原因')
        return
      }
      wx.showLoading({ mask:true,
        title: '请稍候',
      })
      let re = await tool.requestMethod("/com/reject",{
        id:this.properties.item.id,
        account:this.properties.loginedUserInfo.account,
        content:tool.removeBreak(this.data.input)
      })
      wx.hideLoading()
      if(re.status==-2){
        wx.showToast({
          title: '后端服务异常',
          icon: 'none'
        })
        return
      }
      if(re.status==0){
        wx.showToast({
          title: re.content,
          icon: 'none'
        })
        return
      }
      this.refreshHomeList()
      this.setData({
        showOPForm:false
      })
    },

    async CLSubmit(e){
      if(this.data.input==''){
        tool.showToast('请输入留言')
        return
      }
      if(this.data.img.length==0){
        tool.showToast('请上传图片')
        return
      }
      wx.showLoading({ mask:true,
        title: '请稍候',
      })
      let ress = this.data.img
      for(let i=0;i<ress.length;i++){
        ress[i] = await tool.uploadFile(ress[i])
      }
      let re = await tool.requestMethod("/com/deal",{
        id:this.properties.item.id,
        content2:tool.removeBreak(this.data.input),
        image2:ress,
        account2:this.properties.loginedUserInfo.account,
        isDeal:e.currentTarget.dataset.deal
      })
      wx.hideLoading()
      if(re.status==-2){
        wx.showModal({
          title: '提示',
          content: '后端服务异常',
          showCancel: false
        })
        return
      }
      if(re.status==0){
        wx.showModal({
          title: '提示',
          content: re.content,
          showCancel: false
        })
        return
      }
      if(re.status==-1){
        wx.showModal({
          title: '提示',
          content: '此巡查已被删除',
          showCancel: false
        })
        wx.navigateBack()
        return
      }
      await this.hideOPForm()
      this.refreshHomeList()
    },

    async PJSubmit(){
      if(this.data.sat==0){
        tool.showToast('请先评价')
        return
      }
      wx.showLoading({ mask:true,
        title: '请稍候',
      })
      let re = await tool.requestMethod("/com/sat",{
        id:this.properties.item.id,
        sat:this.data.sat
      })
      wx.hideLoading()
      if(re.status==-2){
        wx.showModal({
          title: '提示',
          content: '后端服务异常',
          showCancel: false
        })
        return
      }
      if(re.status==0){
        wx.showModal({
          title: '提示',
          content: re.content,
          showCancel: false
        })
        return
      }
      this.refreshHomeList()
      this.setData({
        showOPForm:false
      })
    },

    hideOPForm(){
      this.setData({
        showOPForm:false,
        img:[],
        input:'',
        sat:0
      })
      this.triggerEvent('scrhd',undefined)
    },
    
    setSat(e){
      this.setData({
        sat:e.currentTarget.dataset.sat
      })
    },

    async CHClick(){
    wx.showModal({
      title: '提示',
      content: '是否撤回此巡查？',
      complete: async (res) => {
        if (res.cancel) {
          
        }
    
        if (res.confirm) {
          wx.showLoading({ mask:true,
            title: '请稍候',
          })
          let re = await tool.requestMethod("/com/withdraw",{
            id:this.properties.item.id
          })
          wx.hideLoading()
          if(re.status==-2){
            wx.showModal({
              title: '提示',
              content: '后端服务异常',
              showCancel: false
            })
            return
          }
          if(re.status==0){
            wx.showModal({
              title: '提示',
              content: re.content,
              showCancel: false
            })
            return
          }
          this.refreshHomeList()
          wx.navigateBack()
        }
      }
    })
    },

    refreshHomeList(){
      this.triggerEvent('refreshList',undefined)
    },

    async PJClick(){
    this.setData({
      showOPForm:true,
      OPFormType:1,
      sat:0
    })
    this.triggerEvent('sctrhd',undefined)
    },

    inputText(e){
    this.setData({
      input:e.detail.value
    })
    },

    async CLClick(){
    this.setData({
      showOPForm:true,
      OPFormType:2,
      input:''
    })
    this.triggerEvent('sctrhd',undefined)
    },

    async SHClick(){
    this.setData({
      showOPForm:true,
      OPFormType:3,
      input:''
    })
    this.triggerEvent('sctrhd',undefined)
    },

    showLocation(){
      wx.openLocation({
        latitude: this.properties.item.lalo[0],
        longitude: this.properties.item.lalo[1],
      })
    },

    showImageViewer(e){
      for(let i=0;i<e.currentTarget.dataset.arr.length;i++){
        e.currentTarget.dataset.arr[i] = config.serverURL + '/file/download/' + e.currentTarget.dataset.arr[i]
      }
      wx.previewImage({
        urls: e.currentTarget.dataset.arr,
        current: config.serverURL + '/file/download/' + e.currentTarget.dataset.current
      })
    },

    showComDetail(){
      let id = this.properties.item.id
      wx.navigateTo({
        url: '/pages/comDetail/conDetail?data=' + JSON.stringify({
          id:id,
          statusBarHeight:this.properties.statusBarHeight,
          contentHeight:this.properties.contentHeight,
          loginedUserInfo:this.properties.loginedUserInfo
        }),
      })
    },

    async cb(){
      let item = this.properties.item
      wx.showLoading({ mask:true,
        title: '请稍候',
      })
      let re = await tool.requestMethod("/com/hasten",{
        id:item.id
      })
      wx.hideLoading()
      if(re.status==-2){
        wx.showModal({
          title: '提示',
          content: '后端服务异常',
          showCancel: false
        })
        return
      }
      if(re.status==0){
        wx.showToast({
          title: re.content,
          icon: 'none'
        })
        return
      }
      wx.showToast({
        title: '催办成功',
        icon: 'none'
      })
    },

  }
})